<?php


namespace app\bm\controller;


use think\Controller;
use think\db\Where;
use think\Request;
use think\Db;
use app\common\model\Bank as BankModel;

class Bank extends Controller
{
    public function index(Request $request)
    {
        $where = new Where();

        if ($request->param('name')) {
            $bank_name = $request->param('name');
            $where['bank_name'] = ['like',"%$bank_name%"];
        }

        if ($request->param('abbr')) {
            $bank_abbr = $request->param('abbr');
            $where['bank_abbr'] = ['like',"%$bank_abbr%"];
        }

        $list = BankModel::where($where)->order("update_time DESC,create_time DESC")->paginate(10);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 数据添加（页面）
     * @return string
     * @throws \Exception
     */
    public function create()
    {
        
        
        return $this->view->fetch();
    }

    public function insert(Request $request)
    {
        if ($request->isPost()) {

            $data = [
                'bank_name'     => $request->post('bank_name'),
                'bank_abbr'     => $request->post('bank_abbr'),
                'bank_uuid'     => uuid()
            ];

            // 实例化验证器
            $validate = new \app\common\validate\Bank();

            if (!$validate->scene('insert')->check($data)) {
                return $this->error($validate->getError());
            }

            // 添加数据到数据库
            $result = BankModel::create($data);

            if ($result) {
                return $this->success("添加成功");
            }

        } else {
            return $this->error("请求方式不被允许 !");
        }
    }

    /**
     * 数据修改页面
     * @param Request $request
     * @return string|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(Request $request)
    {
        $id = $request->param('id');
        if (empty($id)) {
            return $this->error("参数错误");
        }
        $Bank = new BankModel();
        $info = $Bank->getInfo($id);
        if (empty($info)) {
            return $this->error('数据错误或数据不存在');
        }
        $this->assign('info',$info);
        return $this->view->fetch();
    }

    public function update(Request $request)
    {
        if ($request->isPost()) {

            $id = $request->post('id');

            $data = [
                'bank_name' => $request->post('bank_name'),
                'bank_abbr' => $request->post('bank_abbr'),
                'id'=>$id
            ];

            // 实例化验证器
            $validate = new \app\common\validate\Bank();

            if (!$validate->scene('update')->check($data)) {
                dump($validate->getError());
            }

            // 更新数据到数据库
            $result = BankModel::where('id',$id)->update($data);
            if ($result==1) {
                return $this->success("修改成功",url('index'));
            } elseif($result==0) {
                return $this->success("没有需要修改的数据",url('index'));
            } else {
                return $this->error("数据修改失败，请核对递交数据");
            }

        } else {
            return $this->error("请求方式不被允许 !");
        }
    }

    /**
     * 数据删除（软删除）
     * @param Request $request
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        $res = BankModel::destroy($id);
        if ($res) {
            $this->success('删除成功',url('index'));
        }
    }

    /**
     * 回收站
     * @return 软删除数据列表
     */
    public function depot()
    {
        $list = BankModel::onlyTrashed()->select();
        $this->assign("list",$list);
        return $this->view->fetch();
    }

    /**
     * 还原
     * @param $uuid 唯一标识符
     */
    public function restore($uuid)
    {
        if (empty($uuid)) {
            return $this->error('参数错误');
        }
        $bank = BankModel::onlyTrashed()->where('bank_uuid',$uuid)->find();
        $res = $bank->restore();
        if ($res) {
            return $this->success("数据还原成功",url("index"));
        } else {
            return $this->success("数据还原失败",url("index"));
        }
    }
}
